package com.factory.utils;


import java.math.BigInteger;

public class HexUtil {
    /**
     * byte[]转String
     */
    final protected static char[] hexArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public static String byteArrayToHexString(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for (int j = 0; j < bytes.length; j++) {
            v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    /**
     * 十六进制转二进制字符
     */
    public static String hex2bin(String input) {
        StringBuilder sb = new StringBuilder();
        int len = input.length();
        for (int i = 0; i < len; i++) {
            //每1个十六进制位转换为4个二进制位
            String temp = input.substring(i, i + 1);
            int tempInt = Integer.parseInt(temp, 16);
            String tempBin = Integer.toBinaryString(tempInt);
            //如果二进制数不足4位，补0
            if (tempBin.length() < 4) {
                int num = 4 - tempBin.length();
                for (int j = 0; j < num; j++) {
                    sb.append("0");
                }
            }
            sb.append(tempBin);
        }
        return sb.toString();
    }

    //修改指定位进行修改
    public static String modifyBit(String input, int position, boolean value) {
        BigInteger bigInteger = new BigInteger(input, 2);
        BigInteger newValue = value ? bigInteger.setBit(position) : bigInteger.clearBit(position);
        return newValue.toString(2);
    }

    //十进制转换成二进制
    public static String decimalToBinary(int decimal, int length) {
        // 转换为二进制字符串
        String binaryString = Integer.toBinaryString(decimal);
        // 如果二进制字符串长度不足指定长度，补零
        if (binaryString.length() < length) {
            // 使用String.format()方法补齐
            String format = String.format("%1$" + length + "s", binaryString).replace(' ', '0');
            binaryString = format;
        }
        return binaryString;
    }
    //二进制转为16进制
    public static String bitAppoint(String appoint){
        BigInteger bigInteger = new BigInteger(appoint, 2);
        return bigInteger.toString();
    }

    //判断byte长度
    public static String bitLength(String number) {
        String newNumber = Integer.toHexString(Integer.parseInt(number));
        switch (newNumber.length()) {
            case 0:
                newNumber = "0000";
                break;
            case 1:
                newNumber = "000" + newNumber;
                break;
            case 2:
                newNumber = "00" + newNumber;
                break;
            case 3:
                newNumber = "0" + newNumber;
                break;
        }
        System.out.println(newNumber+"十进制转为十六进制");
        return newNumber;
    }
    //String 16进制转byte数组
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }
    public static byte[] floatStringToByteArray(String valueStr) {
        try {
            float value = Float.parseFloat(valueStr);
            int intBits = Float.floatToIntBits(value);
            return new byte[] {
                    (byte) (intBits >> 24),
                    (byte) (intBits >> 16),
                    (byte) (intBits >> 8),
                    (byte) (intBits)
            };
        } catch (NumberFormatException e) {
            // 如果字符串无法解析为 float，可以返回空数组或者抛异常，根据需求处理
            System.err.println("Invalid float string: " + valueStr);
            return new byte[0];  // 或者 throw new IllegalArgumentException("Invalid float string", e);
        }
    }

}
